package com.db.gffxo.epi.repository;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import com.db.gffxo.epi.domain.Event;
import com.db.gffxo.epi.domain.EventDetail;


public class EventDetailDaoGen extends SimpleJdbcDaoSupport implements EventDetailDao {

private final static String QUERY = "SELECT * FROM EventDetail order by detailid";
private final static String REMOVEBYDetailId = "DELETE FROM EventDetail WHERE DetailId = ?";
private final static String QUERYBYDetailId = "SELECT * FROM EventDetail WHERE DetailId = ?";
private final static String INSERT = "INSERT INTO EventDetail(DetailId, EventId, Message, FormatterId,CreateDate,Complete,LastUpdate ) VALUES (eventdetailid.nextval,?,?,?,sysdate,0,sysdate)";
private final static String QUERYbyStatusType="select * from (SELECT d.detailid, e.eventid, d.message, e.eventtype, e.status, e.keyinfo, d.formatterid FROM EventDetail d,Event e where d.eventid=e.eventid and  e.status=? and d.complete=0 and e.EVENTTYPE in ( OURTYPES ) order by detailid) where rownum < ?";
private final static String COMPLETE = "UPDATE EventDetail SET Complete=?, Lastupdate=sysdate WHERE DetailId = ?";



	public void removeByDetailId(Integer DetailId) {
		getSimpleJdbcTemplate().update(REMOVEBYDetailId, DetailId);
	}


	public void insert(EventDetail eventDetail) {
		getSimpleJdbcTemplate().update(INSERT
			,eventDetail.getEventId()
			,eventDetail.getMessage()
			,eventDetail.getFormatterId()
		);
	}

	public void complete(EventDetail eventDetail) {
		int complete = 1;
		
		if (eventDetail.getEvent().getStatus().equals(Event.STATUS_NoPublisher))
			complete = 2;
		
		getSimpleJdbcTemplate().update(COMPLETE
			,complete
			,eventDetail.getDetailId()
		);
	}

	public List getEventDetail() {
		List eventDetails = getSimpleJdbcTemplate().query(QUERY,
			new RowMapper<EventDetail>() {
				public EventDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
				EventDetail eventDetail = new EventDetail();
				eventDetail.setDetailId(rs.getInt(1));
				eventDetail.setEventId(rs.getInt(2));
				eventDetail.setMessage(rs.getString(3));
				eventDetail.setFormatterId(rs.getString(4));
				return eventDetail;
			}
		});
		return eventDetails;
	}
	public List getEventDetailbyDetailId(Integer DetailId) {
		List eventDetails = getSimpleJdbcTemplate().query(QUERYBYDetailId,
			new RowMapper<EventDetail>() {
				public EventDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
				EventDetail eventDetail = new EventDetail();
				eventDetail.setDetailId(rs.getInt(1));
				eventDetail.setEventId(rs.getInt(2));
				eventDetail.setMessage(rs.getString(3));
				eventDetail.setFormatterId(rs.getString(4));
				return eventDetail;
			}
		}
		,DetailId);
		return eventDetails;
	}
	
	public List getEventDetailbyStatusType(String status, String type, int rows) {
		String sqlString = QUERYbyStatusType.replaceFirst("OURTYPES", type);
		List eventDetails = getSimpleJdbcTemplate().query(sqlString,
			new RowMapper<EventDetail>() {
				public EventDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
					EventDetail eventDetail = new EventDetail();
					Event event = new Event();
					eventDetail.setDetailId(rs.getInt(1));
					eventDetail.setEventId(rs.getInt(2));
					eventDetail.setMessage(rs.getString(3));
					event.setEventType(rs.getString(4));
					event.setStatus(rs.getString(5));
					event.setKeyInfo(rs.getString(6));
					eventDetail.setFormatterId(rs.getString(7));
					event.setEventId(eventDetail.getEventId());
					eventDetail.setEvent(event);
					return eventDetail;
				}
			}, status, rows);
		return eventDetails;
	}


}